export function largestValues(root: TreeNode | null): number[] {
  if (!root) return [];
  const res: number[] = [];
  const nodeArr: TreeNode[] = [root];
  while (nodeArr.length) {
    const len = nodeArr.length;
    let maxNum = -Number.MAX_VALUE;
    for (let index = 0; index < len; index++) {
      const node = nodeArr.shift();
      maxNum = Math.max(maxNum, node.val);
      if (node.left) nodeArr.push(node.left);
      if (node.right) nodeArr.push(node.right);
    }
    res.push(maxNum);
  }

  return res;
}
